﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MemoryManagementExercises
{
    class BufferedConsole : IDisposable
    {
        StringBuilder strb;
        int size;

        public BufferedConsole( int size = 25)
        {
            strb = new StringBuilder();
            this.size = size;
        } 
        
        public void Write(string text)
        {
            /*
            strb.Append(text);
            while (strb.Length >= size)
            {
                Console.Write(strb.ToString(0, size));
                strb.Remove(0, size);
            }
            */

            for (int i = 0; i < text.Length; i++)
            {
                strb.Append(text[i]);
                if (strb.Length >= size)
                {
                    Console.Write(strb.ToString());
                    strb.Clear();
                }
            }


        }

        #region IDisposable Members

        public void Dispose()
        {
            Console.Write(strb.ToString());
            strb.Clear();
        }

        ~BufferedConsole()
        {
            Dispose();
        }

        #endregion
    }
}
